# !/usr/bin/env python
# -- coding: utf-8 --
# author：wang time:2024/11/10
import jieba
import re
def analyze_keywords(text,file_path):
    with open(file_path, 'r', encoding='gbk') as f:
        content = f.read()
        cleaned_content = re.sub(r'[^\w\s]', '', content)
        words = jieba.cut(cleaned_content)
    word_count={}
    for word in words:
     if word not in '，。、':
        if word in word_count:
            word_count[word]+=1
        else:
            word_count[word]=1
    word_count_list=list(word_count.items())
    word_count_list.sort(key=lambda x:x[1],reverse=True)
    keywords=[word[0] for word in word_count_list[:10]]
    return keywords
def read_text(file_path):
    f=open(file_path,"r",encoding="gbk")
    f.seek(0)
    text=f.read()
    return text
if __name__=="__main__":
   file_path=r"C:\Users\lx\Pictures\Saved Pictures\如何加强新时代党的建设.txt"
   text=read_text(file_path)
   keywords=analyze_keywords(text,file_path)
   print (keywords)
